package com.mr.web.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.util.CycleDetectionStrategy;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindException;

import com.mr.service.CommonService;
import com.mr.service.DaoService;
import com.mr.service.GoodsService;
import com.mr.web.dwr.DWRUtils;
import com.mr.web.support.DateJsonValueProcessor;
import com.mr.web.support.RequestMapDataBinder;

public class AnnotationController {
	protected static org.apache.log4j.Logger log = org.apache.log4j.Logger
			.getLogger(AnnotationController.class);
	private List<String> validExtension = Arrays.asList("jpg","jpeg","gif","png");
	@Autowired
	@Qualifier("hibernateTemplate")
	protected HibernateTemplate hibernateTemplate;

	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
	}

	@Autowired
	@Qualifier("daoService")
	protected DaoService daoService;

	public void setDaoService(DaoService daoService) {
		this.daoService = daoService;
	}

	@Autowired
	@Qualifier("commonService")
	protected CommonService commonService;

	public void setCommonService(CommonService commonService) {
		this.commonService = commonService;
	}
	@Autowired
	@Qualifier("goodsService")
	protected GoodsService goodsService;

	public void setGoodsService(GoodsService goodsService) {
		this.goodsService = goodsService;
	}
	protected Boolean isRightCK(HttpServletRequest req) {
		String checkcode = req.getParameter("textcode");
		String randck = (String) req.getSession().getAttribute("randck");
		if (StringUtils.equals(checkcode, randck))
			return true;
		return false;
	}

	public String alertMessage(String msg, String returnUrl, ModelMap model) {
		Map jsonMap = new HashMap();
		jsonMap.putAll(model);
		jsonMap.put("msg", msg);
		jsonMap.put("returnUrl", returnUrl);
		model.put("jsonMap", jsonMap);
		return "base/alertMessage.vm";
	}

	protected static BindException bindData(Object command, Map requestParam) {
		RequestMapDataBinder binder = new RequestMapDataBinder(command);
		binder.bind(requestParam);
		BindException errors = new BindException(binder.getBindingResult());
		return errors;
	}

	protected String showJsonSuccess(ModelMap model) {
		model.put("success", true);
		JsonConfig config = new JsonConfig();    
		config.setIgnoreDefaultExcludes(false);       
		config.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);
		config.registerJsonValueProcessor(Date.class, new DateJsonValueProcessor(null));
		JSONObject json = JSONObject.fromObject(model, config);
		model.put("json", json.toString());
		return "base/json.vm";
	}
	public String showJsonSuccess(ModelMap model, String jsname){
	   	Map jsonMap = new HashMap();
	   	jsonMap.putAll(model);
	   	jsonMap.put("success", true);
	   	model.put("jsonMap", jsonMap);
	   	model.put("jsname", jsname);
	   	String result = DWRUtils.getScriptObject(jsname,model);
	   	System.out.println(result);
	   	return "base/dwr.vm";
	}
	protected String showJsonFailure(String msg, ModelMap model) {
		model.put("msg", msg);
		JSONObject json = JSONObject.fromObject(model);
		model.put("json", json.toString());
		return "base/json.vm";
	}

	protected String forwadMessage(ModelMap model, String msg) {
		model.put("msg", msg);
		return "base/showResult.vm";
	}
	protected Boolean validPic(String extname) {
		return validExtension.contains(extname);
	}
	protected Integer getMemberCount(Long id) {
		String hql = "select count(*) from Member m where m.parentid=? and m.id!=m.parentid";
		List list = hibernateTemplate.find(hql, id);
		return Integer.valueOf(list.get(0)+"");
	}
	
}
